class mySort:
    def __init__(self,arr_len):
        self.arr=[3, 87, 2, 93, 78, 56, 61, 38, 12, 40]
        self.arr_len=arr_len

    def test_sort(self,sort_method):
        print(self.arr)
        sort_method()
        print(self.arr)

    def adjust_max_heap(self,dad,arr_len):
        # 将某子树调整为大根堆
        arr=self.arr
        son=dad*2+1
        while son<arr_len:
            if son+1<arr_len and arr[son]<arr[son+1]:
                son+=1
            if arr[dad]<arr[son]:
                arr[dad],arr[son]=arr[son],arr[dad]
                dad=son
                son=dad*2+1
            else:
                break

    def adjust_min_heap(self,dad,arr_len):
        # 将某子树调整为小根堆
        arr=self.arr
        son=dad*2+1
        while son<arr_len:
            if son+1<arr_len and arr[son]>arr[son+1]:
                son+=1
            if arr[dad]>arr[son]:
                arr[dad],arr[son]=arr[son],arr[dad]
                dad=son
                son=dad*2+1
            else:
                break

    # 堆排序
    def heap(self):
        arr=self.arr
        for i in range(self.arr_len//2,-1,-1):
            # self.adjust_max_heap(i,self.arr_len)
            self.adjust_min_heap(i,self.arr_len)
        arr[0],arr[self.arr_len-1]=arr[self.arr_len-1],arr[0]
        for i in range(self.arr_len-1,1,-1):
            # self.adjust_max_heap(0, i)
            self.adjust_min_heap(0,i)
            arr[0], arr[i - 1] = arr[i - 1], arr[0]

if __name__ == '__main__':
    temp=mySort(10)
    temp.test_sort(temp.heap)